package cn.tomoki.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.tomoki.adapters.AppointViewAdapter;
import cn.tomoki.beans.ItemBeanAppoint;
import cn.tomoki.config.URL;
import cn.tomoki.config.UserId;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
 * @author 仓友树
 * 展示正在预约的记录
 */
public class AppointHistoryActivity extends AppCompatActivity {
    public RecyclerView appointHistoryView;
    private ArrayList<ItemBeanAppoint> mDate;
    private AppointViewAdapter adapter;
    private static String userId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.appoint_history);
        userId = UserId.UserId;

        //返回按钮
        ImageButton myBreak =findViewById(R.id.breakButton);
        myBreak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //循环视图
        //找到控件
        appointHistoryView = this.findViewById(R.id.appointHistoryRecyclerView);

        //设置样式
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        appointHistoryView.setLayoutManager(layoutManager);
        //准备数据
        /**
         * 模拟数据
         */
        initDate();


    }

    private void initListener() {
        adapter.setOnItemClickListener(new AppointViewAdapter.OnItemClickListener(){
            @Override
            public void onItemClick(int position) {
                //这里处理点击事件，该干嘛就干嘛
                ItemBeanAppoint itemBeanAppoint = mDate.get(position);
                Intent intent = new Intent(AppointHistoryActivity.this, AppointHistoryDetailsActivity.class);
                intent.putExtra("mark",itemBeanAppoint.mark);
                startActivity(intent);
            }
        });
    }
    private void initDate() {
        //创建数据集合
        mDate = new ArrayList<>();

        //接收后端数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String url = (URL.url + "/app/getAppointAllByUserId?userId="+userId);
                    //创造HTTP客户端
                    OkHttpClient client = new OkHttpClient()
                            .newBuilder()
                            .connectTimeout(200, TimeUnit.SECONDS)
                            .readTimeout(250, TimeUnit.SECONDS)
                            .build();
                    //创造http请求
                    Request request = new Request.Builder()
                            .url(url)
                            .build();
                    //执行发送的指令
                    Response response = client.newCall(request).execute();
                    String responseDate = response.body().string();
                    JSONArray jsonArray = new JSONArray(responseDate);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        //创建数据对象
                        ItemBeanAppoint date = new ItemBeanAppoint();
                        date.icon = R.drawable.next;
                        date.mark = jsonObject.getInt("mark");
                        date.appointTime = jsonObject.getString("time");
                        date.appointName = jsonObject.getString("name");
                        date.appointNumber = jsonObject.getString("number")+"件";
                        mDate.add(date);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //创建适配器
                            adapter = new AppointViewAdapter(mDate);
                            //设置到view里面
                            appointHistoryView.setAdapter(adapter);
                            //初始化事件
                            initListener();
                        }
                    });

                }catch (Exception e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AppointHistoryActivity.this,"连接不上网络啦(。﹏。)",Toast.LENGTH_SHORT);
                        }
                    });
                }
            }
        }).start();
    }

}